﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using System.ComponentModel;
using Agen.ServiceAgencyWPF;
using Agen.Models;
using System.Diagnostics;
using System.Collections.ObjectModel;
using System.IO;

namespace Agen.Views
{
    public partial class Factures : Page
    {
        private FacturesVO vo;
        private Stream clientStream; 
        public Factures()
        {
            InitializeComponent();
            vo = new FacturesVO();
            DataContext = vo;
            ApplicationManager.client.GetRecapFacturesCustomerCompleted += new EventHandler<GetRecapFacturesCustomerCompletedEventArgs>(client_GetRecapFacturesCustomerCompleted);
            ApplicationManager.client.PayUserBillCompleted += new EventHandler<AsyncCompletedEventArgs>(client_PayUserBillCompleted);
            ApplicationManager.client.getFatcureCompleted += new EventHandler<getFatcureCompletedEventArgs>(client_getFatcureCompleted);
        }

        void client_getFatcureCompleted(object sender, getFatcureCompletedEventArgs e)
        {
            using (clientStream)
            {
                byte[] info = e.Result;
                clientStream.Write(info, 0, info.Length);
                clientStream.Close();
            }
        }

        void client_PayUserBillCompleted(object sender, AsyncCompletedEventArgs e)
        {
            this.vo.detailList = new Dictionary<int, List<FactureElement>>() {
                                                                                {0, new List<FactureElement>()},
                                                                                {1, new List<FactureElement>()},
                                                                                {4, new List<FactureElement>()},
                                                                                {5, new List<FactureElement>()},
                                                                                {6, new List<FactureElement>()},
                                                                                {7, new List<FactureElement>()},
                                                                                {16, new List<FactureElement>()}
                                                                            };
            ApplicationManager.client.GetRecapFacturesCustomerAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }

        void client_GetRecapFacturesCustomerCompleted(object sender, GetRecapFacturesCustomerCompletedEventArgs e)
        {
            this.vo.Recap = e.Result;
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            ApplicationManager.client.GetRecapFacturesCustomerAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer);
        }

        private void select_ckb_Checked(object sender, RoutedEventArgs e)
        {
            this.vo.SessionsToPay.Add((sender as CheckBox).Tag as SessionToPayDC);
            CreateDetailsPayment();
        }

        private void select_ckb_Unchecked(object sender, RoutedEventArgs e)
        {
            SessionToPayDC sessionSelected = (sender as CheckBox).Tag as SessionToPayDC;
            this.vo.SessionsToPay.Remove(sessionSelected);
            CreateDetailsPayment();
        }


        private void CreateDetailsPayment() {
            detail_zone.Children.Clear();
            this.vo.detailList = new Dictionary<int, List<FactureElement>>() {
                {0, new List<FactureElement>()},
                {1, new List<FactureElement>()},
                {4, new List<FactureElement>()},
                {5, new List<FactureElement>()},
                {6, new List<FactureElement>()},
                {7, new List<FactureElement>()},
                {16, new List<FactureElement>()}
            };
            int total_price = 0;
            List<Label> listLabels = new List<Label>();
            foreach (SessionToPayDC session in this.vo.SessionsToPay)
            {
                List<FactureElement> listElement = new List<FactureElement>();
                if (this.vo.detailList.Keys.Contains(session.IdFormation))
                {
                    listElement = this.vo.detailList[session.IdFormation] as List<FactureElement>;
                }
                else {
                    listElement = this.vo.detailList[0] as List<FactureElement>;                
                }

                FactureElement element = new FactureElement();
                element.name = session.Name;
                element.price = session.Price;
                listElement.Add(element);

                FactureElement elementReplacement = new FactureElement();


                switch (session.IdSession)
                {
                    case 1 :
                        if(this.vo.detailList[1].Count == 3){
                            List<FactureElement> listElementDefault = this.vo.detailList[0] as List<FactureElement>;
                            elementReplacement.name = "3 sessions of 3 hours";
                            elementReplacement.price = 450;
                            (this.vo.detailList[1] as List<FactureElement>).Clear();
                        }
                        break;
                    case 4 :
                        if (this.vo.detailList[4].Count == 3)
                        {
                            List<FactureElement> listElementDefault = this.vo.detailList[6] as List<FactureElement>;
                            elementReplacement.name = "3 hours on “plateau";
                            elementReplacement.price = 150;
                            (this.vo.detailList[4] as List<FactureElement>).Clear();
                        }
                        if ((this.vo.detailList[4].Count+this.vo.detailList[5].Count+this.vo.detailList[6].Count*3) == 10)
                        {
                            List<FactureElement> listElementDefault = this.vo.detailList[7] as List<FactureElement>;
                            elementReplacement.name = "Package including 10 hours of driving";
                            elementReplacement.price = 460;
                            (this.vo.detailList[4] as List<FactureElement>).Clear();
                            (this.vo.detailList[5] as List<FactureElement>).Clear();
                            (this.vo.detailList[6] as List<FactureElement>).Clear();
                        }
                        if ((this.vo.detailList[4].Count + this.vo.detailList[5].Count + this.vo.detailList[6].Count * 3 + this.vo.detailList[7].Count * 10) == 20)
                        {
                            List<FactureElement> listElementDefault = this.vo.detailList[0] as List<FactureElement>;
                            elementReplacement.name = "20 hours of courses";
                            elementReplacement.price = 990;
                            (this.vo.detailList[4] as List<FactureElement>).Clear();
                            (this.vo.detailList[5] as List<FactureElement>).Clear();
                            (this.vo.detailList[6] as List<FactureElement>).Clear();
                            (this.vo.detailList[7] as List<FactureElement>).Clear();
                        }
                        break;
                    case 5 :
                        if ((this.vo.detailList[4].Count+this.vo.detailList[5].Count+this.vo.detailList[6].Count*3) == 10)
                        {
                            List<FactureElement> listElementDefault = this.vo.detailList[7] as List<FactureElement>;
                            elementReplacement.name = "Package including 10 hours of driving";
                            elementReplacement.price = 460;
                            (this.vo.detailList[4] as List<FactureElement>).Clear();
                            (this.vo.detailList[5] as List<FactureElement>).Clear();
                            (this.vo.detailList[6] as List<FactureElement>).Clear();
                        }
                        if ((this.vo.detailList[4].Count + this.vo.detailList[5].Count + this.vo.detailList[6].Count * 3 + this.vo.detailList[7].Count * 10) == 20)
                        {
                            List<FactureElement> listElementDefault = this.vo.detailList[0] as List<FactureElement>;
                            elementReplacement.name = "20 hours of courses";
                            elementReplacement.price = 990;
                            (this.vo.detailList[4] as List<FactureElement>).Clear();
                            (this.vo.detailList[5] as List<FactureElement>).Clear();
                            (this.vo.detailList[6] as List<FactureElement>).Clear();
                            (this.vo.detailList[7] as List<FactureElement>).Clear();
                        }
                        break;
                    case 16 :
                        if (this.vo.detailList[16].Count == 10)
                        {
                            List<FactureElement> listElementDefault = this.vo.detailList[0] as List<FactureElement>;
                            elementReplacement.name = "10-hour driving package";
                            elementReplacement.price = 450;
                            (this.vo.detailList[16] as List<FactureElement>).Clear();
                        }
                        break;
                }


            }
            foreach (KeyValuePair<int, List<FactureElement>> item in this.vo.detailList)
            {
                foreach (FactureElement sousElement in item.Value)
                {
                    total_price += sousElement.price;
                    Label label = new Label();
                    label.Content = sousElement.name  + "   " + sousElement.price;
                    listLabels.Add(label);
                }
            }
            Label labelprice = new Label();
            labelprice.Content = "Total :  " + total_price+" Euros";
            listLabels.Add(labelprice);
            detail_zone.Children.Clear();
            foreach (Label lab in listLabels)
            {
                detail_zone.Children.Add(lab);
            }
        }

        private void generateFacture_btn_Click(object sender, RoutedEventArgs e)
        {
            ObservableCollection<string> list = new ObservableCollection<string>();
            foreach (KeyValuePair<int, List<FactureElement>> item in this.vo.detailList)
            {
                foreach (FactureElement sousElement in item.Value)
                {
                    string elementFacture = sousElement.name + "@" + Convert.ToString(sousElement.price);
                    list.Add(elementFacture);
                }
            }



            ApplicationManager.client.PayUserBillAsync(ApplicationManager.CURRENT_CUSTOMER.IdCustomer, list, this.vo.SessionsToPay);
        }

        private void cancel_button_Click(object sender, RoutedEventArgs e)
        {
            ApplicationManager.FrameAction(this, ApplicationManager.PREVIOUS_HANDLE, new Object());
        }

        private void getFacture_btn_Click(object sender, RoutedEventArgs e)
        {
            SaveFileDialog popup = new SaveFileDialog();
            popup.Filter = "PFD File (.pdf)|*.pdf";
            bool? ret = popup.ShowDialog();
            if (ret == true)
            {
               clientStream = (Stream)popup.OpenFile();
            }
            FactureDC facture = (sender as Button).Tag as FactureDC;
            ApplicationManager.client.getFatcureAsync(facture.Id);
        }
    }

    public class FacturesVO : INotifyPropertyChanged
    {
        private RecapFacturesDC _recap;

        private ObservableCollection<SessionToPayDC> _sessionsToPay;

        public FacturesVO() {
            this.Recap = new RecapFacturesDC();
            this.SessionsToPay = new ObservableCollection<SessionToPayDC>();
        }

        public Dictionary<int, List<FactureElement>> detailList = new Dictionary<int, List<FactureElement>>() {
            {0, new List<FactureElement>()},
            {1, new List<FactureElement>()},
            {4, new List<FactureElement>()},
            {5, new List<FactureElement>()},
            {6, new List<FactureElement>()},
            {7, new List<FactureElement>()},
            {16, new List<FactureElement>()}
        };

        public RecapFacturesDC Recap
        {
            get
            {
                return this._recap;
            }
            set
            {
                this._recap = value;
                OnPropertyChanged("Recap");
            }
        }

        public ObservableCollection<SessionToPayDC> SessionsToPay
        {             
            get
            {
                return this._sessionsToPay;
            }
            set
            {
                this._sessionsToPay = value;
                OnPropertyChanged("SessionsToPay");
            } 
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }


    public class FactureElement{

        public string name { get; set; }
        public int price { get; set; }
        
    }

}
